/**
 * @Title: RedisController.java
 * @Description: 用一句话描述该文件做什么
 * @author zhangxin
 * @date 2019年3月13日
 */
package com.fortune.rainshinecommon.controller;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.enums.ResultCode;

@RestController
@RequestMapping("common")
public class RedisController {
	@Autowired
	private StringRedisTemplate stringRedisTemplate;// 操作字符串
	@Autowired
	private RedisTemplate redisTemplate;// redisTemplate
	
	//根据key获取value
	@GetMapping("/get/{key}")
    public MsgModel get(@PathVariable(name = "key") String key) {
        Object o = null;
        if (exists(key)) {
            ValueOperations<String, String> os = stringRedisTemplate.opsForValue();
            o = os.get(key);
        }
        return new MsgModel(ResultCode.SUCCESS,o);
    }
	
	//向redis里存入数据永不過期
	@PostMapping("/set/{key}/{value}")
    public boolean set(@PathVariable(name = "key") String key, @PathVariable(name = "value") String value) {
        boolean result = false;
        try {
            ValueOperations<String, String> os = stringRedisTemplate.opsForValue();
            os.set(key, value);
            result = stringRedisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
	
	@PostMapping("/set/{key}/{value}/{time}")
	 public boolean set(@PathVariable(name = "key") String key, @PathVariable(name = "value") String value,@PathVariable(name = "time") Integer time) {
	        boolean result = false;
	        try {
	            stringRedisTemplate.opsForValue().set(key, value,time,TimeUnit.SECONDS);
	            result = stringRedisTemplate.hasKey(key);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return result;
	    }
	
	@GetMapping("/delete/{key}")
    public boolean delete(@PathVariable(name = "key") String key) {
        boolean result = false;
        if (exists(key)) {
        	stringRedisTemplate.delete(key);
            result = true;
        }
        return result;
    }
	
	@GetMapping("/updateTime/{key}")
    public boolean updateTime(@PathVariable(name = "key") String key,@PathVariable(name = "time") Integer time) {
        boolean result = false;
        if (exists(key)) {
        	stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
            result = true;
        }
        return result;
    }
	
	@GetMapping("/exists/{key}")
    public boolean exists(@PathVariable(name = "key") String key) {
        return stringRedisTemplate.hasKey(key);
    }
	
	@GetMapping("/postEntity/{key}/{object}")
	public boolean postEntity(@PathVariable(name = "key") String key,@PathVariable(name = "object") Object o,@PathVariable(name = "time") Integer time) {
		boolean result = false;
		try {
			redisTemplate.opsForValue().set(key, o);
            result = stringRedisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
	}

	@GetMapping("/getEntity/{key}")
	public Object getEntity(@PathVariable(name = "key") String key,@PathVariable(name = "object") Object o) {
		return redisTemplate.opsForValue().get(key);
	}

}
